/*
day223
2020.4.7
*/
#include<vector>
#include<string>
#include<map>
#include<iostream>
using namespace std;

class Solution {
public:
    int balancedString(string s) {
        map<char,int> m;
		for(int i=0;i<s.size();++i)
		{
			m[s[i]]++;
		}
		
		
		int avg= s.size()/4;
		for(auto iter=m.begin();iter!=m.end();iter++)
		{
			iter->second = iter->second>avg ? iter->second-avg : 0;
		}

		
		if(m['Q']==0 && m['W']==0 && m['E']==0 && m['R']==0)
		{
			return 0;
		}
		
		int res=m['Q']+m['W']+m['E']+m['R'];
		
		for(int len=res;len<=s.size();++len)
		{
			for(int i=0;i+len<=s.size();++i)
			{
				map<char,int> tem;
				for(int q=0;q<len;q++)
				{
					tem[s[i+q]]++;
				}
				if(
				tem['Q']>=m['Q'] && 
				tem['W']>=m['W'] &&
				tem['E']>=m['E'] &&
				tem['R']>=m['R'])   
				{
					res=len;
					return len;	
				}         
			}
		}
		return res;
    }
};

int main()
{
	string s = "RRWREWREWQREQWEQEQQRERREWRQRWWERRWRWWERQQERWQREEWERWRQQRRWQQWWQE";
	Solution sl ;
	cout<<sl.balancedString(s)<<endl;
	return 0;
}
